;-------------------------------------------------------------LICENSE--------------------------------------------------------------;
;                                                                                                                                  ;
;The MAP code is written in Fortran language for magnetohydrodynamics (MHD) calculation with the adaptive mesh refinement (AMR)    ;
;and Message Passing Interface (MPI) parallelization.                                                                              ;
;                                                                                                                                  ;
;Copyright (C) 2012                                                                                                                ;
;Ronglin Jiang                                                                                                                     ;
;rljiang@ssc.net.cn                                                                                                                ;
;585 Guoshoujing Road. Pudong, Shanghai, P.R.C. 201203                                                                             ;
;                                                                                                                                  ;
;This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License         ;
;as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.             ;
;                                                                                                                                  ;
;This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of    ;
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.                        ;
;                                                                                                                                  ;
;You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software     ;
;Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.                                                   ;
;                                                                                                                                  ;
;-------------------------------------------------------------LICENSE--------------------------------------------------------------;

;======================================================================================================================|
function colorbar_obj, omodel, rgbtable = rgbtable, range = range, color = color, title_c = title_c,  $
  title_size = title_size, pos = pos, vertical = vertical, text_dir_change = text_dir_change, win_ratio = win_ratio,  $
  width = width, length = length, log = log
;======================================================================================================================|
;  Purpose
;    Plot a colorbar for IDL object system
;    
;  Syntax
;    colorbar_model = colorbar_obj(omodel [, rgbtable = rgbtable] [, range = range] [, color = color]  $
;      [, title_c = title_c] [,title_size = title_size] [, pos = pos] [ win_ratio = win_ratio]  $
;      [, width = width] [, length = length] [, /vertical] [, /log] [, /text_dir_change,] )
;
;  Inputs
;    omodel: the parent object of colorbar which will be created
;    rgbtable: color table
;    range: the range of colorbar
;    color: the color of the title, tick and lable of the colorbar
;    title_c: the title of the colorbar
;    title_size: the size of the title (normal units)
;    pos: the lower left position (normal units)
;    win_ratio: the ratio between the xsize and ysize of output window
;    width: a factor to adjust the width (normal units)
;    length: a factor to adjust the width (this is a factor. just a factor, do not ask why)
;    vertical: vertical colorbar
;    log: log colorbar
;    text_dir_change: change the direction of the label and title
;
;  Outputs
;    a object model include colorbar
;
;  Examples
;    omodel = colorbar_obj(omodel)
;
;  Notes
;
;  History
;    2010-06-25 written by R. L. Jiang at Kwasan Observatory
;    
;  Bug report
;    rljiang@nju.edu.cn
;======================================================================================================================|

  on_error, 2
  
  if size(omodel, /tname) ne 'OBJREF' then begin
    print, ' The parent model of the colorbar should be given.'
    retall
  endif
  if n_elements(rgbtable) ne 256*3 then tvlct, rgbtable, /get
  if n_elements(range) ne 2 then range = [0, 255]
  if n_elements(color) ne 3 then color = [255, 255, 255]
  if n_elements(title_c) eq 0 then title_c = 'Colorbar'
  if n_elements(title_size) eq 0 then title_size = 0.08
  if n_elements(pos) ne 2 then pos = [0.5, 0.5]
  if not keyword_set(vertical) then vertical = 0
  if not keyword_set(text_dir_change) then text_dir_change = 0
  if not keyword_set(log) then log = 0
  if n_elements(win_ratio) eq 0 then win_ratio = 1
  if n_elements(width) eq 0 then width = 0.0625
  if n_elements(length) eq 0 then length = 1.0
  
  omodel  =  obj_new('idlgrmodel')
  
  range_log = range
  if keyword_set(log) then range_log = alog10(range_log)
  rl = float(range_log[1]-range_log[0])
  
  if vertical eq 0 then begin
    xdim = 256
    ydim = round(width*xdim)
    maxdim = float(max([xdim, ydim]))
    rgb  =  bytarr(3, xdim, ydim)
    for i  =  0, ydim-1 do begin
      rgb[0, *, i]  =  rgbtable[*, 0]
      rgb[1, *, i]  =  rgbtable[*, 1]
      rgb[2, *, i]  =  rgbtable[*, 2]
    endfor
    xc = [(pos[0]-0.5)*2-range_log[0]/rl*length, 1.0/rl*length]
    yc = [(pos[1]-0.5)*2+ydim/maxdim*text_dir_change*length, 1.0/maxdim*length]
    xcc = [(pos[0]-0.5)*2, 1.0/maxdim*length]
    ycc = [(pos[1]-0.5)*2, 1.0/maxdim*length]
    title_size = [rl/sqrt(win_ratio), maxdim*sqrt(win_ratio)]*title_size
    ticklen = ydim/5
  endif else begin
    win_ratio = 1.0/win_ratio
    ydim = 256
    xdim = round(width*ydim)
    maxdim = float(max([xdim, ydim]))
    rgb  =  bytarr(3, xdim, ydim)
    for i  =  0, xdim-1 do begin
      rgb[0, i, *]  =  rgbtable[*, 0]
      rgb[1, i, *]  =  rgbtable[*, 1]
      rgb[2, i, *]  =  rgbtable[*, 2]
    endfor
    xc = [(pos[0]-0.5)*2+xdim/maxdim*text_dir_change*length, 1.0/maxdim*length]
    yc = [(pos[1]-0.5)*2-range_log[0]/rl*length, 1.0/rl*length]
    xcc = [(pos[0]-0.5)*2, 1.0/maxdim*length]
    ycc = [(pos[1]-0.5)*2, 1.0/maxdim*length]
    title_size = [rl/sqrt(win_ratio), maxdim*sqrt(win_ratio)]*title_size
    ticklen = xdim/5
  endelse
  oimage  =  obj_new('idlgrimage', rgb)
  ocbtitle  =  obj_new('idlgrtext', title_c)
  oaxis  =  obj_new('idlgraxis', vertical, /exact)
  oline = obj_new('idlgrpolyline', [0, 0, xdim, xdim, 0], [0, ydim, ydim, 0, 0])
  ocbtitle->setproperty, char_dimensions = title_size
  oimage->setproperty, dimensions = [xdim, ydim], xcoord_conv = xcc, ycoord_conv = ycc
  oaxis->setproperty, color = color, log = log, range = range, ticklen = ticklen, title = ocbtitle, textpos = text_dir_change, tickdir = 1-text_dir_change
  oaxis->setproperty, xcoord_conv = xc, ycoord_conv = yc
  oline->setproperty, color = color, xcoord_conv = xcc, ycoord_conv = ycc
  
  oaxis->getproperty, major = major, minor = minor
  if major lt 3 then begin
    major = 5
    oaxis->setproperty, major = major
  endif
  oaxis->getproperty, ticktext = ticktext
  ticktext->getproperty, string = aa
  if keyword_set(vertical) then title_size = reverse(title_size)
  ticktext->setproperty, char_dimensions = title_size*0.66
    
  omodel->add, oimage
  omodel->add, oaxis
  omodel->add, oline

  return, omodel

end